// 定义存储的键名
const STORAGE_KEYS = {
  WATER_COUNT: 'water_count',
  LAST_DRINK_TIME: 'last_drink_time'
} as const

// 存储相关的工具函数
const StorageUtil = {
  // 获取喝水次数
  getWaterCount(): number {
    try {
      const count = wx.getStorageSync(STORAGE_KEYS.WATER_COUNT)
      return count || 0
    } catch (e) {
      console.error('获取喝水次数失败:', e)
      return 0
    }
  },

  // 设置喝水次数
  setWaterCount(count: number): void {
    try {
      wx.setStorageSync(STORAGE_KEYS.WATER_COUNT, count)
    } catch (e) {
      console.error('保存喝水次数失败:', e)
    }
  },

  // 获取最后喝水时间
  getLastDrinkTime(): Date | null {
    try {
      const timeStr = wx.getStorageSync(STORAGE_KEYS.LAST_DRINK_TIME)
      return timeStr ? new Date(timeStr) : null
    } catch (e) {
      console.error('获取最后喝水时间失败:', e)
      return null
    }
  },

  // 设置最后喝水时间
  setLastDrinkTime(time: Date | null): void {
    try {
      if (!time) {
        wx.removeStorageSync(STORAGE_KEYS.LAST_DRINK_TIME)
        return
      }
      wx.setStorageSync(STORAGE_KEYS.LAST_DRINK_TIME, time.toISOString())
    } catch (e) {
      console.error('保存最后喝水时间失败:', e)
    }
  },

  getTotalMl(): number {
    return wx.getStorageSync('totalMl') || 0
  },

  setTotalMl(ml: number) {
    wx.setStorageSync('totalMl', ml)
  },

  getMlPerDrink(): number {
    return wx.getStorageSync('mlPerDrink') || 200
  },

  setMlPerDrink(ml: number) {
    wx.setStorageSync('mlPerDrink', ml)
  }
}

export default StorageUtil 